package com.vq3.digitalizationsoftwareintegrates.Service.DataCenter.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vq3.digitalizationsoftwareintegrates.Entity.DataCenter.DataCenterEntity;
import com.vq3.digitalizationsoftwareintegrates.Entity.DataCenter.DataCenter_permissionEntity;
import com.vq3.digitalizationsoftwareintegrates.Entity.Spot_Check.S_C_PointEntity;
import com.vq3.digitalizationsoftwareintegrates.Entity.Spot_Check.S_C_Point_ContentEntity;
import com.vq3.digitalizationsoftwareintegrates.Entity.User.Permission_TBEntity;
import com.vq3.digitalizationsoftwareintegrates.Entity.User.User_RoleEntity;
import com.vq3.digitalizationsoftwareintegrates.Mapper.DataCenter.DataCenterMapper;
import com.vq3.digitalizationsoftwareintegrates.Mapper.DataCenter.DataCenter_permissionMapper;
import com.vq3.digitalizationsoftwareintegrates.Mapper.Spot_Check.S_C_PointMapper;
import com.vq3.digitalizationsoftwareintegrates.Mapper.Spot_Check.S_C_Point_ContentMapper;
import com.vq3.digitalizationsoftwareintegrates.Service.DataCenter.DataCenterService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
public class DataCenterServiceImpl implements DataCenterService {
    @Resource
    DataCenterMapper dataCenterMapper;
    @Resource
    DataCenter_permissionMapper dataCenter_permissionMapper;
    @Resource
    S_C_PointMapper s_c_pointMapper;
    @Resource
    S_C_Point_ContentMapper s_c_point_contentMapper;

    @Override
    public DataCenterEntity findDataCenterSideBar(Integer did) {
        QueryWrapper<DataCenterEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",did);
        return dataCenterMapper.selectOne(queryWrapper);


    }

    public boolean deleteSCData(List<String> pointNameArr, List<Integer> groupIdArr){
        boolean flag = false;
        try {


        for (int i = 0; i < pointNameArr.size(); i++) {
            System.out.println(groupIdArr.get(i));
            UpdateWrapper<S_C_PointEntity> s_c_pointEntityQueryWrapper = new UpdateWrapper<>();
            String replace = pointNameArr.get(i).replace("\"", "").replace("[","").replace("]","");
            s_c_pointEntityQueryWrapper.eq("point_name",replace);
            S_C_PointEntity s_c_pointEntity = new S_C_PointEntity();
            s_c_pointEntity.setDeleteStatus(0);


            int update = s_c_pointMapper.update(s_c_pointEntity, s_c_pointEntityQueryWrapper);
            flag = update >= 1;

        }
        }catch (Exception e){
            e.printStackTrace();
        }
        return flag;
    }

    public IPage<S_C_Point_ContentEntity> dailySCData(String pointName,int page) {
        IPage<S_C_Point_ContentEntity> s_c_point_contentEntityIPage = new Page<>(page,10);
        QueryWrapper<S_C_Point_ContentEntity> s_c_point_contentEntityQueryWrapper = new QueryWrapper<>();
        s_c_point_contentEntityQueryWrapper.eq("point_name",pointName);
        return s_c_point_contentMapper.selectPage(s_c_point_contentEntityIPage,s_c_point_contentEntityQueryWrapper);
    }

    public boolean deleteDailySCData(List<String> dailyPointNameS,List<String> dailyPointContextS) {
        boolean flag = false;
        for (String dailyPointName : dailyPointNameS) {
            for (String dailyPointContext : dailyPointContextS) {
                QueryWrapper<S_C_Point_ContentEntity> sCPointContentEntityQueryWrapper = new QueryWrapper<>();
                UpdateWrapper<S_C_Point_ContentEntity> updateWrapper = new UpdateWrapper<>();
                updateWrapper.eq("point_content", dailyPointContext).eq("point_name", dailyPointName);
                S_C_Point_ContentEntity s_c_point_contentEntity = new S_C_Point_ContentEntity();

                s_c_point_contentEntity.setScDeletestatus(0);
                int update = s_c_point_contentMapper.update(s_c_point_contentEntity, sCPointContentEntityQueryWrapper);
                flag = update > 1;
            }

        }
        return flag;

    }

    public boolean reversSCDataGroup(List<Integer> groupS,String pointName) {
        boolean flag = false;
        System.out.println(groupS);
        QueryWrapper<S_C_PointEntity> s_c_pointEntityQueryWrapper = new QueryWrapper<>();
        s_c_pointEntityQueryWrapper.eq("point_name",pointName);
        List<S_C_PointEntity> s_c_pointEntities = s_c_pointMapper.selectList(s_c_pointEntityQueryWrapper);
        List<S_C_PointEntity> s_c_pointEntities1 = new ArrayList<>();
        s_c_pointMapper.delete(s_c_pointEntityQueryWrapper);

        // TODO: 2023/2/7 遍历的数组锁住了
        for (Integer group : groupS) {

            for (S_C_PointEntity sCPointEntity : s_c_pointEntities) {
                S_C_PointEntity s_c_pointEntity = new S_C_PointEntity();
                s_c_pointEntity.setGroupId(group);
                s_c_pointEntity.setPointId(sCPointEntity.getPointId());
                s_c_pointEntity.setUpdateTime(new Date());
                s_c_pointEntity.setPointContentId(sCPointEntity.getPointContentId());
                s_c_pointEntity.setPointName(sCPointEntity.getPointName());
                s_c_pointEntity.setCreateTime(sCPointEntity.getCreateTime());
                s_c_pointEntity.setStatusType(sCPointEntity.getStatusType());
                s_c_pointEntity.setPointCount(sCPointEntity.getPointCount());
                s_c_pointEntity.setDeleteStatus(sCPointEntity.getDeleteStatus());

                s_c_pointEntities1.add(s_c_pointEntity);
            }


        }
        for (S_C_PointEntity s_c_pointEntity : s_c_pointEntities1) {
            int insert = s_c_pointMapper.insert(s_c_pointEntity);
            System.out.println(s_c_pointEntity);
            flag = insert > 0;
        }

        return flag;
    }

    public boolean dailyEdit(String dailyEdit_pointContent,String pointName,String pointContentInput, String scFunInput, String scBenchmarkInput){
        QueryWrapper<S_C_Point_ContentEntity> sCPointContentEntityQueryWrapper = new QueryWrapper<>();
        UpdateWrapper<S_C_Point_ContentEntity> s_c_point_contentEntityUpdateWrapper = new UpdateWrapper<>();
        s_c_point_contentEntityUpdateWrapper.eq("point_name",pointName).eq("point_content",dailyEdit_pointContent);
        sCPointContentEntityQueryWrapper.eq("point_name",pointName).eq("point_content",dailyEdit_pointContent);
        List<S_C_Point_ContentEntity> s_c_point_contentEntitiesNew = new ArrayList<>();
        List<S_C_Point_ContentEntity> s_c_point_contentEntities = s_c_point_contentMapper.selectList(sCPointContentEntityQueryWrapper);
        System.out.println(pointContentInput+"->"+scFunInput+"->"+scBenchmarkInput);
        for (S_C_Point_ContentEntity s_c_point_contentEntity : s_c_point_contentEntities) {
            S_C_Point_ContentEntity s_c_point_contentEntity1 = new S_C_Point_ContentEntity();

            s_c_point_contentEntity1.setPointContentId(s_c_point_contentEntity.getPointContentId());
            s_c_point_contentEntity1.setPointName(s_c_point_contentEntity.getPointName());
            s_c_point_contentEntity1.setPointContentStandard(s_c_point_contentEntity.getPointContentStandard());
            if ("".equalsIgnoreCase(pointContentInput)){
                s_c_point_contentEntity1.setPointContent(s_c_point_contentEntity.getPointContent());
            }else {
                s_c_point_contentEntity1.setPointContent(pointContentInput);
            }
            if ("".equalsIgnoreCase(scFunInput)){
                s_c_point_contentEntity1.setScFun(s_c_point_contentEntity.getScFun());
            }else {
                s_c_point_contentEntity1.setScFun(scFunInput);
            }
            if ("".equalsIgnoreCase(scBenchmarkInput)){
                s_c_point_contentEntity1.setScBenchmark(s_c_point_contentEntity.getScBenchmark());
            }else {
                s_c_point_contentEntity1.setScBenchmark(scBenchmarkInput);
            }
            s_c_point_contentEntitiesNew.add(s_c_point_contentEntity1);
        }
        boolean flag = false;
        for (S_C_Point_ContentEntity s_c_point_contentEntity : s_c_point_contentEntitiesNew) {
            int update = s_c_point_contentMapper.update(s_c_point_contentEntity, s_c_point_contentEntityUpdateWrapper);
            if (!(update > 0)) {
                flag = false;
                break;
            }else {
                flag = true;
            }
        }
        return flag;
    }

    @Override
    public DataCenter_permissionEntity perIdFindDataCenter_Per(String permission) {
        QueryWrapper<DataCenter_permissionEntity> dataCenter_permissionEntityQueryWrapper = new QueryWrapper<>();
        dataCenter_permissionEntityQueryWrapper.eq("pid",permission);
        return dataCenter_permissionMapper.selectOne(dataCenter_permissionEntityQueryWrapper);

    }

    @Override
    public List<DataCenter_permissionEntity> perfindDataCenterid(Permission_TBEntity permission_tbEntities) {
        QueryWrapper<DataCenter_permissionEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid",permission_tbEntities.getPermission());
        return dataCenter_permissionMapper.selectList(queryWrapper);
    }
}
